package com.sdd.sndd;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * ClassName: NacosConfigSender
 * Description:
 * date: 2020/7/1
 *
 * @author xugaojun
 */
public class NacosConfigSender {

    public static void main(String[] args) throws Exception {
        final String remoteAddress = "127.0.0.1";
        final String groupId = "Sentinel:Nacos-Demo";
        final String dataId = "com.sdd.sndd";
        // 构造类级别的规则
        List<FlowRule> rules = new ArrayList<>();
        FlowRule flowRule = new FlowRule();
        flowRule.setResource("TestResource");
        flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT); // 快速失败
        flowRule.setCount(5);
        flowRule.setLimitApp("default");
        flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT);
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rules.add(flowRule);
        String rule = JSON.toJSONString(rules);
        System.out.println(rule);
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.SERVER_ADDR, remoteAddress);
        properties.setProperty(PropertyKeyConst.NAMESPACE, "9a33f60e-4cb4-44a7-a532-3efabffa253c");
        ConfigService configService = NacosFactory.createConfigService(properties);
        System.out.println(configService.publishConfig(dataId, groupId, rule));
    }

}
